<?php
	
	namespace Aspic\Form\Element;
	use Aspic as A;
	use Aspic\Security\Security as S;

	/**
	* Filterable form element add ability to filter data that is return with getValue() (and getValues from the surrounding container)
	* Filter names could be found in DataFilter class.
	 * @todo Make this a Trait and an interface
	*/
	abstract class AbstractFilterableElement extends AbstractDataElement {
	
		/**
		* @var array Filters of the element like: array('filterName', array('param1', 'param2'))
		*/
		protected $_filters;
		
		public function __construct($name, array $options = array(), array $attributes = array()) {
			$this->_filters = array();
			
			parent::__construct($name, $options, $attributes);
		}
		
		public function addFilter($filterName, array $params = array()) {
			$this->_filters[] = array($filterName, $params);
		}
		
		public function hasFilters() {
			return (bool)(count($this->opt('filters')));
		}
		
		public function getValue(array $form = null) {
			if($this->hasFilters()) {
				$value = parent::getValue($form);
				$filteredValue = A\DataFilter::filter($value, $this->opt('filters'), true);
				
				return $filteredValue;
			}
			else {
				return parent::getValue($form);
			}
		}
		
	}
	
?>